package adke.models.policies;

import adke.Util;
import adke.managers.DropManager;
import adke.managers.GamePowerupManager;
import adke.models.DropPolicy;
import adke.models.powerups.Powerup;
import java.util.Timer;
import java.util.TimerTask;

// drop met random intervallen random powerups

// de policy is in deze mate customizable:
// tijdsspan kan verander worden
// er moet gekozen kunnen worden of we alleen abilitypowerups toelaten.
// of propertypowerups
// of allebei

public class RandomTimedPolicy extends DropPolicy
{
    private Timer timer;
    
    private int timeRange;
    
    private boolean abilityAllowed, propertyAllowed;
    
    public RandomTimedPolicy(int dropPolicy)
    {
        super(dropPolicy);
        
        abilityAllowed = true;
        propertyAllowed = true;
        timeRange = 60;
//        timeRange = 10;
    }

    // in seconden
    public void setFrequencyRange(int range)
    {
        timeRange = range;
    }
    
    public void setAbilityPowerupAllowed(boolean allowed)
    {
        abilityAllowed = allowed;
    }
    
    public void setPropertyPowerupAllowed(boolean allowed)
    {
        propertyAllowed = allowed;
    }
    
    public void startPolicy()
    {
        System.out.println("Drop policy started: RandomTimerPolicy");
        
        timer = new Timer();
        
        // start droptimer
//        int newDrop = 2 + Util.random(timeRange);
        int newDrop = 2;
        timer.schedule(new RandomDrop(), (long)(newDrop * 1000));
    }
    
    
    // deze taak wordt recursief aangeroepen.
    // telkens wanneer ereen powerup is gedropt wordt
    // met deze taak epaald wanneer er weer een nieuwe komt
    private class RandomDrop extends TimerTask
    {
        public void run()
        {
            // drop random powerup
            if (DropManager.getDroppedPowerupCount() == 0 && GamePowerupManager.getGamePowerupCount() > 0)
            {
                Powerup randomPower = GamePowerupManager.getRandomGamePowerup(abilityAllowed, propertyAllowed);
                DropManager.dropPowerup(randomPower);
            }

            // prepare nieuwe drop
            int newDrop = 2 + Util.random(timeRange);
            timer = new Timer();
            timer.schedule(new RandomDrop(), (long)(newDrop * 1000));
        }
    }
    
    public void stopPolicy()
    {
        if (timer != null)
            timer.cancel();
        timer = null;
    }
}
